<?php
/**
 * Created by PhpStorm.
 * User: xuho
 * Date: 2020-02-15
 * Time: 19:37
 */

namespace app\cdc\validate\admin;


use app\common\model\mysql\AccountModel;

class StoreValidate extends \app\common\validate\BaseValidate
{
    protected $rule = [
        'name' => 'require',
        'realname' => 'require',
        'mobile' => 'max:11|regex:/^1[3-9]{1}[0-9]{9}$/|checkMobile',
        'address' => 'require',
        'status' => 'require'
    ];

    //错误信息
    protected $message = [
        'name.require' => '请输入 商户名',
        'realname.require' => '真实姓名 必填',
        'mobile.regex' => '手机号码格式错误',
        'mobile.max' => '手机号码格式错误',
        'address.require' => '地址 必填',
        'status.require' => '请选择 状态',
    ];

    protected $scene = [
        'save' => ['name', 'realname', 'mobile', 'address'],
        'update' => ['name', 'realname', 'mobile', 'address', 'status'],
    ];

    protected function checkMobile($value, $rule, $data = [])
    {
        $where[] = ['mobile', '=', $data['mobile']];
        !empty($data['account_id']) ? $where[] = ['id', '<>', $data['account_id']] : false;
        if (AccountModel::checkRowExists($where)) {
            return '手机号码已存在';
        }
        return true;
    }
}